<template>
	<div class="container">
		<el-dialog
			title="添加用户"
			:close-on-click-modal="false"
			:close-on-press-escape="false"
			destroy-on-close
			v-model="isShowDialog"
			:show-close="false"
			width="720px"
		>
			<el-form :model="ruleForm" :rules="rules" ref="userForm" size="small" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户名" prop="account">
							<el-input v-model="ruleForm.account" placeholder="请输入用户名" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="phone">
							<el-input v-model="ruleForm.phone" maxlength="11" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="密码" prop="password">
							<el-input v-model="ruleForm.password" type="password" placeholder="请输入密码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="密码确认" prop="repassword">
							<el-input v-model="ruleForm.repassword" type="password" placeholder="请再次输入密码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="昵称" prop="nickName">
							<el-input v-model="ruleForm.nickName" placeholder="请输入昵称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设置角色" prop="roleId">
							<el-cascader
								@change="changeRole"
								style="width:100%"
								placeholder="请选择角色"
								v-model="ruleForm.roleId"
								:props="{
									value: 'id',
									label: 'roleName',
									checkStrictly: true
								}"
								:options="role_list"
								filterable
								clearable
							/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="small">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="small">提 交</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, watch, ref, defineComponent } from 'vue';
import { ElMessageBox } from 'element-plus';
import { add } from '/@/api/platform/user/index'
import { getList } from '/@/api/platform/role/index'
import { Md5 } from 'ts-md5';
export default defineComponent({
	name: 'AddUser',
	setup(props: any, context: any) {
		const state = reactive({
			isShowDialog: false,
			ruleForm: {
				account: "",
				password: "",
				repassword: "",
				phone: "",
				nickName: "",
				//默认生日，用户登录自行修改
				birthday: new Date(),
				email: "",
				//默认一个特定头像图片链接，用户登录后自己修改头像信息
				avatar: "upload/img/default.jpg",
				roleId: [],
			},
			rules: {
				account: [{
					required: true,
					message: "账号不能为空！",
					trigger: "blur"
				}],
				password: [{
					required: true,
					message: "密码不能为空！",
					trigger: "blur"
				}],
				repassword: [{
					required: true,
					validator: (rule: any, value: any, callback: any) => {
						if (value === '') {
							callback(new Error('请确认密码！'))
						} else if (value !== state.ruleForm.password) {
							callback(new Error("两次输入的密码不一致！"))
						} else {
							callback()
						}
					},
					trigger: "blur"
				}],
				phone: [{
					required: true,
					message: "手机号不能为空",
					trigger: "blur"
				}],
				roleId: [{
					required: true,
					validator: (rule: any, value: any, callback: any) => {
						if (value === null || value.length <= 0) {
							callback(new Error('请选择用户绑定的角色'))
						} else {
							callback()
						}
					},
					trigger: "blur"
				}]
			},
			role_list: []
		});
		const userForm = ref();
		const getRoles = () => {
			getList({}).then((res) => {
				state.role_list = res.data.rows;
			})

		};
		// 打开弹窗
		const openDialog = (row?: object) => {
			state.isShowDialog = true;
			getRoles();
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
			initForm();
		};
		const initForm = () => {
			userForm.value.resetFields();
		};
		// 新增
		const onSubmit = () => {
			userForm.value.validate((valid: boolean) => {
				if (!valid) {
					return false;
				}
				state.ruleForm.password = Md5Password(state.ruleForm.password);
				state.ruleForm.roleId = state.ruleForm.roleId[0];
				add(state.ruleForm).then((res) => {
					ElMessageBox.alert(res.Message, '提示', {}).then(() => {
						// 刷新父级数据
						context.emit("sendRefresh");
						closeDialog(); // 关闭弹窗
						initForm();
					}).catch(() => { });
				})
			})
		};
		const Md5Password = (value: any) => {
			return Md5.hashStr(value);
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
			userForm
		};
	},
});
</script>